package com.wolfword.crawler.server.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.wolfword.common.domain.Result;
import com.wolfword.common.domain.ResultConstant;
import com.wolfword.common.util.object.StringUtil;
import com.wolfword.crawler.server.entity.ClientLogEntity;
import com.wolfword.crawler.server.service.ClientLogService;

/**
 * 客户端状态相关
 * @author Server Zhang
 * @date 2018-01-11 10:54:52
 */
@RestController
@RequestMapping("/client/log")
@Api(value = "/client/log", description = "客户端状态相关")
public class ClientLogController {
	
	private final Logger logger = LoggerFactory.getLogger(ClientLogController.class);

	@Autowired
	private ClientLogService clientLogService;
	
	@ApiOperation("提交客户端状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientIp", value = "客户端IP", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "clientPort", value = "客户端端口", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "clientStatus", value = "状态", paramType = "query", dataType = "Integer", required = true)
    })
	@PostMapping("addClientStatus")
	public void addClientStatus(String clientIp, Integer clientPort, Integer clientStatus){
		if (StringUtil.isEmpty(clientIp,clientPort,clientStatus)) {
			return;
		}
		clientLogService.addClientStatus(clientIp, clientPort, clientStatus);
	}
	
	/**
	 * 客户端列表
     * @param request
     * @param clientName
     * @param clientIp
     * @param clientPort
     * @param status
     * @param pageIndex
     * @param pageSize
     * @return
     * @author ServerZhang
     * @date 2018年1月24日
     */
    @ApiOperation("客户端列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientName", value = "客户端名称", paramType = "query", dataType = "string", required = false),
            @ApiImplicitParam(name = "clientIp", value = "IP", paramType = "query", dataType = "string", required = false),
            @ApiImplicitParam(name = "clientPort", value = "端口", paramType = "query", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "status", value = "爬取状态：0断开，1待机，2工作中", paramType = "query", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "pageIndex", value = "页码", paramType = "query", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", dataType = "Integer", required = false)
    })
	@PostMapping(value="/list")
	public Result list(HttpServletRequest request,
			String clientName, String clientIp, Integer clientPort,
			@RequestParam(value="status",defaultValue="-1")Integer status,
			@RequestParam(value="pageIndex",defaultValue="0")Integer pageIndex,
			@RequestParam(value="pageSize",defaultValue="50") Integer pageSize){
    	
    	if (status.equals(-1)) {
			status = null;
		}
    	
    	List<ClientLogEntity> list = null;
    	Result result = null;
		try {
			list = clientLogService.list(clientName, clientIp, clientPort, status, pageIndex, pageSize);
			result = new Result(ResultConstant.SUCCESS,new PageInfo<>(list));
		} catch (Exception e) {
			logger.info(e.getMessage());
			e.printStackTrace();
			result = new Result(ResultConstant.GENERAL_ERROR.setMessage(e.getMessage()));
		}
		
	    return result;
	} 
}
